

# 阿里云账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维，请登录RAM控制台创建RAM用户。
import oss2

BUCKET_NAME = "sheledon-bucket"
auth = oss2.Auth('LTAI5tDBTb2se9cqcEAh1QEK', 'Ov13MOJDrbijIQKUWjFabX3cGmjhlP')
filePath = "py_project_oss/user/"
BASE_URL = "https://sheledon-bucket.oss-cn-beijing.aliyuncs.com/py_project_oss/user/"

# yourEndpoint填写Bucket所在地域对应的Endpoint。以华东1（杭州）为例，Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。
endpoint = 'oss-cn-beijing.aliyuncs.com'

def upload_file(baseName,file):
    try:
        bucket = oss2.Bucket(auth, endpoint, BUCKET_NAME, connect_timeout=30)
        # 填写Object完整路径和Bytes内容。Object完整路径中不能包含Bucket名称。
        suffix=file.name.split('.')[-1]
        fileName=filePath+baseName+'.'+suffix
        bucket.put_object(filePath+fileName+'.'+suffix,file.chunks())
        # 返回图片URL
        return BASE_URL+fileName
    except Exception as err:
        print(err)
    return None